﻿@using Microsoft.AspNetCore.Mvc.Localization
@model OverviewViewModel
@inject IViewLocalizer Localizer
@{
    ViewData["Title"] = @Localizer["Title"];
}
<br />
@if (Model != null)
{
    <div class="tilegrid">
        @foreach (ChargePointsOverviewViewModel cpvm in Model.ChargePoints)
        {

            string chargePointName = string.IsNullOrWhiteSpace(cpvm.Name) ? $"{cpvm.ChargePointId}:{cpvm.ConnectorId}" : cpvm.Name;
            string lastCharge = (cpvm.MeterStart >= 0 && cpvm.MeterStop != null) ? string.Format(Localizer["ChargekWh"].Value, (cpvm.MeterStop - cpvm.MeterStart)) : null;
            string chargeTime = null;
            if (cpvm.StartTime != null && cpvm.StopTime == null)
            {
                TimeSpan timeSpan = DateTime.UtcNow.Subtract(cpvm.StartTime.Value);
                chargeTime = string.Format(Localizer["ChargeTime"].Value, (timeSpan.Days*24 + timeSpan.Hours), timeSpan.Minutes);
            }

            string cpIcon = "fa-plug";
            string cpColor = "successColor";
            string cpTitle = Localizer["Available"].Value;
            switch (cpvm.ConnectorStatus)
            {
                case ConnectorStatusEnum.Occupied:
                    cpIcon = "fa-bolt"; //"fa-car";
                    cpColor = "errorColor";
                    cpTitle = Localizer["Charging"].Value;
                    break;
                case ConnectorStatusEnum.Faulted:
                    cpIcon = "fa-times-circle";
                    cpColor = "unavailableColor";
                    cpTitle = Localizer["Faulted"].Value;
                    break;
                case ConnectorStatusEnum.Unavailable:
                    cpIcon = "fa-ban";
                    cpColor = "unavailableColor";
                    cpTitle = Localizer["Unavailable"].Value;
                    break;
            }

            <div class="card border-secondary" style="max-width: 18rem;">
                <a href="~/Home/Transactions/@Uri.EscapeDataString(cpvm.ChargePointId)/@cpvm.ConnectorId" class="text-decoration-none">
                    <div class="card-header @cpColor">
                        <i class="fas @cpIcon fa-2x"></i> @chargePointName
                    </div>
                    <div class="card-body text-secondary">
                        <h5 class="card-title">@cpTitle</h5>
                        @if (!string.IsNullOrEmpty(chargeTime))
                        {
                            <p class="card-text">@chargeTime</p>
                        }
                        else if (!string.IsNullOrEmpty(lastCharge))
                        {
                            <p class="card-text">@lastCharge</p>
                        }
                        else
                        {
                            <p class="card-text">&nbsp;</p>
                        }
                    </div>
                    @if (Model.ServerConnection)
                    {
                        <div class="card-footer text-muted d-flex justify-content-between">
                            <div>@cpvm.CurrentChargeData</div>
                            @if (cpvm.Online)
                            {
                                <div><i class="fas fa-link" title="@Localizer["ChargePointOnline"]"></i></div>
                            }
                            else
                            {
                                <div><i class="fas fa-unlink" title="@Localizer["ChargePointOffline"]"></i></div>
                            }
                        </div>
                    }
                </a>
            </div>
        }
    </div>
    @if (!string.IsNullOrEmpty(ViewBag.ErrorMsg))
    {
        <br/>
        <div class="alert alert-danger" role="alert">
            @ViewBag.ErrorMsg
        </div>
    }
}

